package cn.edu.bupt.ch6_2.controller;

import cn.edu.bupt.ch6_2.common.Result;
import cn.edu.bupt.ch6_2.dto.ProductWithComments;
import cn.edu.bupt.ch6_2.entity.Comment;
import cn.edu.bupt.ch6_2.entity.Product;
import cn.edu.bupt.ch6_2.service.ICommentService;
import cn.edu.bupt.ch6_2.service.IProductService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@Tag(name = "商品及评论接口")
@RestController
@RequestMapping("/api/v1/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private ICommentService commentService;

    @Operation(summary = "获取商品列表", description = "返回值为全部的商品列表")
    @GetMapping(path = "/", produces = "application/json")
    public Result<?> listAll() {
        List<Product> products = productService.findAll();
        return Result.ok(products);
    }

    @Operation(summary = "按名称模糊查找商品及所有评论", description = "嵌套查询")
    @GetMapping(path = "/findByNameWithComments", produces = "application/json")
    public Result<?> findByNameWithComments(String name) {
        List<ProductWithComments> productWithComments = productService.findByNameWithComments(name);
        return Result.ok(productWithComments);
    }

    @Operation(summary = "按名称模糊查找商品及所有评论列表V2", description = "映射嵌套结果集合")
    @GetMapping(path = "/findByNameWithCommentsV2", produces = "application/json")
    public Result<?> findByNameWithCommentsV2(String name) {
        List<ProductWithComments> productWithComments = productService.findByNameWithCommentsV2(name);
        return Result.ok(productWithComments);
    }

    @Operation(summary = "添加新的商品", description = "创建成功，返回状态码201")
    @PostMapping(path = "/", consumes = "application/json", produces = "application/json")
    public Result<?>  add(@RequestBody Product product) {
        productService.create(product);
        return Result.ok(product);
    }

    @Operation(summary = "根据id查找商品", description = "如果不存在，则返回状态码204，如果存在且删除成功，则返回状态码204")
    @GetMapping(path = "/{prodId}")
    public Result<?> get(@PathVariable Long prodId) {
        Product product = productService.findById(prodId);
        if (product!= null) {
            return Result.ok(product);
        } else {
            return Result.error(204, "该商品不存在");
        }
    }

    @Operation(summary = "根据id删除商品", description = "如果不存在，则返回状态码404，如果存在且删除成功，则返回状态码204")
    @DeleteMapping(path = "/{prodId}")
    public Result<?> delete(@PathVariable Long prodId) {
        int count = productService.deleteById(prodId);
        if (count>0) {
            return Result.ok();
        } else {
            return Result.error(204, "该商品不存在");
        }
    }

    @Operation(summary = "根据id更新商品", description = "如果不存在，则返回状态码204，如果存在且删除成功，则返回状态码204")
    @PutMapping(path = "/{prodId}")
    public Result<?> edit(@PathVariable Long prodId, @RequestBody Product product) {
        product.setId(prodId);
        product = productService.updateReturnProduct(product);
        if (product!=null) {
            return Result.ok(product);
        } else {
            return Result.error(204, "该商品不存在");
        }
    }

    @Operation(summary = "获取商品评论列表", description = "返回值为商品的评论列表")
    @GetMapping(path = "/{prodId}/comment/", produces = "application/json")
    public Result<?> commentListAll(@PathVariable Long prodId) {
        List<Comment> comments = commentService.findByProdId(prodId);
        if (comments!= null) {
            return Result.ok(comments);
        } else {
            return Result.error(204, "评论不存在");
        }
    }

    @Operation(summary = "为商品添加评论", description = "创建成功，返回状态码201")
    @PostMapping(path = "/{prodId}/comment/", consumes = "application/json", produces = "application/json")
    public Result<?>  commentAdd(@PathVariable Long prodId, @RequestBody Comment comment) {
        Product product = productService.findById(prodId);
        if (product != null) {
            comment.setProductId(prodId);
            commentService.create(comment);
            return Result.ok(comment);
        } else {
            return Result.error(204, "该商品不存在");
        }
    }
}
